package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
@CrossOrigin
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list).message("获取列表成功");
    }

    @ApiOperation(value = "根据id删除积分等级",notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value="数据id",example = "100",required = true)
            @PathVariable Long id){
        boolean result = integralGradeService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "新增积分等级")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){
        //if (integralGrade.getBorrowAmount() == null){
        //    throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        //}
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean flag = integralGradeService.save(integralGrade);
        if(flag){return R.ok().message("保存成功");}
        else {return R.error().message("保存失败");}
    }

    @ApiOperation(value = "根据id查询积分等级")
    @GetMapping("/get/{id}")
    public R getIntegralGradeById(
            @ApiParam(value = "查询id",required = true)
            @PathVariable Integer id){
        IntegralGrade result = integralGradeService.getById(id);
        if (result != null){
            return R.ok().data("record",result);
        }else {
            return R.error().message("获取数据失败");
        }
    }

    @ApiOperation(value = "修改积分等级")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){
        boolean flag = integralGradeService.updateById(integralGrade);
        if(flag){return R.ok().message("更新成功");}
        else {return R.error().message("更新失败");}
    }

}
